using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(艺术字Model))]
public class 艺术字工具 : Editor
{
    private 艺术字Model model;
    private Enum艺术字key枚举 eee;
    private Texture addTexture;
    private void OnEnable()
    {
        model = target as 艺术字Model;
    }


    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        
        EditorGUILayout.TextArea("编辑器======================================编辑器");
        EditorGUILayout.TextArea("=================================================");
        EditorGUILayout.TextArea("=================================================");
        EditorGUILayout.TextArea("编辑器======================================编辑器");
        eee = (Enum艺术字key枚举)EditorGUILayout.EnumPopup("新增字体类型", eee);
        addTexture = EditorGUILayout.ObjectField("你说过", addTexture, typeof(Texture)) as Texture;
        if (GUILayout.Button("新增"))
        {
            var path = AssetDatabase.GetAssetPath(addTexture);
            Debug.Log(path);
            Debug.Log(eee);
            var allspri = AssetDatabase.LoadAllAssetsAtPath(path).Where(w=>w is Sprite).Select(s=>(Sprite)s).ToList();
            if (allspri.Count > 0)
            {
                艺术字体 a = new 艺术字体();
                a.type = eee;
                List<艺术字体.字体> res = new List<艺术字体.字体>();
                // Debug.Log(allspri[0].GetType());
                int index = 0;
                foreach (var VARIABLE in allspri)
                {
                    Debug.Log(VARIABLE);
                    string cc = "a";
                    if (index < 10)
                    {
                        cc = index.ToString();
                    }
                    
                    var size = VARIABLE.bounds.size * 100;
                    Debug.Log(size);
                    int x = (int)size.x;
                    int y = (int)size.y;

                    res.Add(new 艺术字体.字体(){key = cc,value = VARIABLE,x = x,y = y});
                    index++;
                }
                
                a.all字 = res;
                
                var f = typeof(艺术字Model).GetField("Datas", BindingFlags.NonPublic | BindingFlags.Instance);
                var ddddatas = (IEnumerable<dynamic>)(f.GetValue(model));
                List<艺术字体> newdata = new List<艺术字体>();
                foreach (var VARIABLE in ddddatas)
                {
                    newdata.Add(VARIABLE);
                }
                newdata.Add(a);

                f.SetValue(model,newdata);
                EditorUtility.SetDirty(model);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("这里面没有sprite");
            }
        }

        if (GUILayout.Button("检查所有字体的大小"))
        {
            foreach (var VARIABLE in model.GetData())
            {
                foreach (var zz in VARIABLE.all字)
                {
                    var size = zz.value.bounds.size * 100;
                    Debug.Log(size);
                    zz.x = (int)size.x;
                    zz.y = (int)size.y;
                }
            }
        }
    }
}
